<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>新建一个数组</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array</span> &mdash; <span class="dc-title">
     新建一个数组
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array</strong></span>
    ([ <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ] ) : <span class="type">array</span></div>

    <p class="para rdfs-comment">
     创建一个数组。关于数组是什么的信息请阅读<a href="language.types.array.html" class="link">数组</a>一节。
    </p>
   </div>

 <div class="refsect1 parameters" id="refsect1-function.array-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

    <p class="para">
     语法“index =&gt; values”，用逗号分开，定义了索引和值。索引可以是字符串或数字。如果省略了索引，会自动产生从
     0 开始的整数索引。如果索引是整数，则下一个产生的索引将是目前最大的整数索引
     + 1。注意如果定义了两个完全一样的索引，则后面一个会覆盖前一个。
    </p>
    <p class="para">
     在最后一个定义的数组项目之后加一个逗号虽然不常见，却是合法的语法。
    </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
     返回根据参数建立的数组。参数可以用 <em>=&gt;</em>
     运算符给出索引。关于数组是什么的信息请阅读<a href="language.types.array.html" class="link">数组</a>一节。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-examples">
  <h3 class="title">范例</h3>
  <p class="para">
     下面的例子演示了怎样建立一个二维数组，怎样给相应的数组指定键名，以及怎样在普通数组中略过和继续数字索引。
   <div class="example" id="example-6083">
    <p><strong>Example #1 <span class="function"><strong>array()</strong></span>  例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fruits&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"fruits"&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"banana"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"numbers"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"holes"&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">"first"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"second"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"third"</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6084">
    <p><strong>Example #2  <span class="function"><strong>array()</strong></span> 的自动索引</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">19</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">13</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 1
    [1] =&gt; 1
    [2] =&gt; 1
    [3] =&gt; 13
    [4] =&gt; 1
    [8] =&gt; 1
    [9] =&gt; 19
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
     注意索引 3 被定义了两次，保留了最后的值 13。索引 4 在 索引
     8 之后定义，下一个自动生成的索引（值为 19 那个）为 9，因为最大的索引是 8。
  </p>
  <p class="para">
   本例建立了从 1 开始的数组。
   <div class="example" id="example-6085">
    <p><strong>Example #3 从 1 开始索引的 <span class="function"><strong>array()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$firstquarter&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'January'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'February'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'March'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$firstquarter</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [1] =&gt; January
    [2] =&gt; February
    [3] =&gt; March
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   在 Perl 中，可以访问在双引号内的数组的值。但在 PHP
     中需要将数组用花括号括起来。
   <div class="example" id="example-6086">
    <p><strong>Example #4 访问双引号内的数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'bar'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$foo</span><span style="color: #007700">[</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">]}</span><span style="color: #DD0000">!"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;Hello&nbsp;baz!<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.array-notes">
  <h3 class="title">注释</h3>
  <p class="para">
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
      <span class="function"><strong>array()</strong></span> 是一个语言结构，用于字面上表示数组，不是常规的函数。
    </p>
   </p></blockquote>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_pad.html" class="function" rel="rdfs-seeAlso">array_pad()</a> - 以指定长度将一个值填充进数组</span></li>
    <li class="member"><span class="function"><a href="list.html" class="function" rel="rdfs-seeAlso">list()</a> - 把数组中的值赋给一组变量</span></li>
    <li class="member"><span class="function"><a href="count.html" class="function" rel="rdfs-seeAlso">count()</a> - 计算数组中的单元数目，或对象中的属性个数</span></li>
    <li class="member"><span class="function"><a href="range.html" class="function" rel="rdfs-seeAlso">range()</a> - 根据范围创建数组，包含指定的元素</span></li>
    <li class="member"><a href="control_structures.foreach.html" class="link">foreach</a></li>
    <li class="member">The <a href="language.types.array.html" class="link">array</a> type</li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>